<template>
  <div>
    <!-- 导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>留言管理</el-breadcrumb-item>
      <el-breadcrumb-item>留言列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input
            placeholder="请输入要查询的留言人"
            v-model="username"
            clearable
            @clear="getBorrowByPerson"
            @keyup.enter.native="getBorrowByPerson"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getBorrowByPerson"
            ></el-button></el-input
        ></el-col>
      </el-row>
      <el-table :data="CommentList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="username" label="名称"></el-table-column>
        <el-table-column prop="phone" label="手机号码"></el-table-column>
        <el-table-column prop="content" label="评论内容"></el-table-column>
        <el-table-column prop="create_time" label="发布时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="removeCommentById(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { FormatTime } from "../../utils/utils";
export default {
  data() {
    return {
      CommentList: [],
      username: "",
    };
  },
  created() {
    this.getBorrowsList();
  },
  methods: {
    async getBorrowsList() {
      let res = await this.$http.get("comment");
      if (res.status !== 200) {
        return this.$message.error(res.statusText);
      }

      this.CommentList = res.data.map((item) => {
        return { ...item, create_time: FormatTime(item.create_time) };
      });
      console.log(this.CommentList);
    },
    async removeCommentById(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该留言, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((error) => error);
      if (confirmResult !== "confirm") {
        return this.$message.info("取消了删除！");
      }
      let res = await this.$http.delete(`comment/${id}`);
      if (res.data.status !== 200) {
        return this.$message.error(res.data.message);
      }
      this.getBorrowsList();
      this.$message.success(res.data.message);
    },
    //搜索功能
    async getBorrowByPerson() {
      let username = this.username;
      let res = await this.$http.post("comment/search", {
        username,
      });
      if (res.data.status !== 200) {
        return this.$message.error(res.data.message);
      }
      console.log(res);
      this.CommentList = res.data.CommentList.map((item) => {
        return { ...item, create_time: FormatTime(item.create_time) };
      });
      console.log(this.CommentList);
      this.$message.success(res.data.message);
    },
  },
};
</script>

<style lang="less" scope>
.el-tag {
  margin-top: 15px;
}
.bt {
  border-top: 1px solid #eee;
}
.bb {
  border-bottom: 1px solid #eee;
}
.vercenter {
  display: flex;
  align-items: center;
}
</style>